//
//  TitleSelectionView.m
//  QA
//
//  Created by Yoga on 2017/5/2.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "TitleSelectionView.h"

static NSInteger titleSelectionBaseTag = 1828381;

@interface TitleSelectionView ()
@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) UIView *lineView;
//@property (nonatomic, strong) UIColor *backColor;     // 背景颜色
@property (nonatomic, strong) UIColor *textColor;       // 按钮颜色
@property (nonatomic, strong) UIColor *selectTextColor; // 按钮选中颜色

@property (nonatomic, strong) UIButton *tempButton;     // 上一次选中的按钮
@end

@implementation TitleSelectionView

- (instancetype)initWithBackColor:(UIColor *)backColor TextColor:(UIColor *)textColor SelectTextColor:(UIColor *)selectTextColor{
    self = [super init];
    if (self) {
        _titleArr = [NSArray array];
        _currentIndex = 0;
        self.backgroundColor = backColor;
        self.textColor = textColor;
        self.selectTextColor = selectTextColor;
    }
    return self;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _titleArr = [NSArray array];
        _currentIndex = 0;
        self.backgroundColor = [UIColor clearColor];
        self.textColor = [UIColor borderColor];
        self.selectTextColor = [UIColor ThemeColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
//    CGFloat commonWidth = self.width / self.titleArr.count;
//    for (NSInteger i = 0 ; i < self.titleArr.count; i++) {
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        button.frame = CGRectMake(i * commonWidth, 0, commonWidth, self.height);
//        [button setTitle:[self.titleArr safeObjectAtIndex:i] forState:UIControlStateNormal];
//        [button setTitleColor:self.textColor forState:UIControlStateNormal];
//        [button.titleLabel setFont:[UIFont systemFontOfSize:16.0f]];
//        button.tag = titleSelectionBaseTag + i;
//        
//        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
//        
//        [self addSubview:button];
//    }
//    [self addSubview:self.lineView];
}

- (void)layoutSubviews {
    [super layoutSubviews];
}

- (void)setTitleWithArrs:(NSArray *)arr withShowIndex:(NSInteger)index {
    self.titleArr = arr;
    CGFloat commonWidth = self.width / self.titleArr.count;
    self.currentIndex = index;
    self.lineView.center = CGPointMake((index + 0.5) * commonWidth, self.height * 0.5 + 15);
    
    for (NSInteger i = 0 ; i < self.titleArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i * commonWidth, 0, commonWidth, self.height);
        [button setTitle:[self.titleArr safeObjectAtIndex:i] forState:UIControlStateNormal];
        [button setTitleColor:self.textColor forState:UIControlStateNormal];
        [button setTitleColor:self.selectTextColor forState:UIControlStateSelected];
        [button.titleLabel setFont:[UIFont systemFontOfSize:14.0f]];
        button.tag = titleSelectionBaseTag + i;
        if (i == self.currentIndex) {
            [button setSelected:YES];
            self.tempButton = button;
        }
        
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:button];
        
        if (i < self.titleArr.count-1) {            
            // 灰线
            CGFloat height = self.height*7.0/17.0;
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(commonWidth-0.5, (self.height-height)/2.0, 0.5, height)];
            [line setBackgroundColor:[UIColor colorWithHex:0x2F2F34 alpha:1.0]];
            [button addSubview:line];
        }
    }
    [self addSubview:self.lineView];
}

- (void)buttonClick:(UIButton *)sender {
    
    NSInteger index = sender.tag - titleSelectionBaseTag;
    self.currentIndex = index;
    [self animationForLine:index];
    if (self.block) {
        self.block(index);
    }
}

- (void)animationForLine:(NSInteger)index {
    UIButton *sender = (UIButton *)[self viewWithTag:(index+titleSelectionBaseTag)];
    if (self.tempButton != sender) {
        [self.tempButton setSelected:NO];
        self.tempButton = sender;
    }
    [sender setSelected:YES];
    
    CGFloat commonWidth = self.width / self.titleArr.count;

    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.lineView.center = CGPointMake((index + 0.5) * commonWidth, weakSelf.lineView.centerY);
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark - getters
- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [UIView new];
        _lineView.backgroundColor = [UIColor ThemeColor];
        _lineView.size = CGSizeMake(40, 2);
//        _lineView.layer.cornerRadius = 2;
    }
    return _lineView;
}

@end
